import { useState, useEffect } from "react"

import Swiper from "./Swiper"
import Item from "./Item"

import http from "../../request"
import store from "../../store"

function WellReceived(props) {
  const [swiperData, setSwiperData] = useState([])
  const [list, setList] = useState([])

  useEffect(() => {
    const sub = store.subscribe(() => {
      getWellReceived()
    })
    getSwiper()
    getWellReceived()
    return () => {sub()}
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])

  return(
    <div style={{height:'93vh',overflow:'scroll'}}>
      <Swiper data={swiperData}/>
      <div className="u-m-b-50">
        {list.map(i => <Item key={i.id} data={i} type='WellReceived' setVideo={props.video}/>)}
      </div>
    </div>
  )
  
  function getSwiper() {
    http('/swiper').then(res => {
      if(res.data.data.returnCode === '0') {
        setSwiperData(res.data.data.returnValue)
        console.log(swiperData);
      }
    }).catch(err => console.log(err))
  }

  function getWellReceived() {
    let city = ''
    switch (store.getState().city) {
      case '北京': city = 'bj'
        break;
      case '上海': city = 'sh'
        break;
      case '广州': city = 'gz'
        break;
      default:break;
    }
    http('/hot/', 'get', {params:{city}}).then(res => {
      if(res.data.data.returnCode === '0') {
        setList(res.data.data.returnValue)
      }
      console.log(list);
    }).catch(err => console.log(err))
  }
}

export default WellReceived